package com.woniu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.Client;
import com.woniu.service.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 处理客户列表请求
 */
@Api(value = "客户列表", tags = {"客户管理"})
@RestController
@RequestMapping("/client")
public class ClientController extends BaseController {
    @Autowired
    ClientService clientService;

    @ApiOperation(value = "导出客户列表",response= Client.class, httpMethod="GET", tags = {"客户管理"})
    @PreAuthorize("@ss.hasPermi('test:export')")
    @Log(title = "客戶管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Client client) throws IOException {
        List<Client> list = clientService.exportList();
        ExcelUtil<Client> util = new ExcelUtil<>(Client.class);
        util.exportExcel(response,list,"客戶列表信息");
    }

    @ApiOperation(value = "查询所有的客户列表",response= Client.class, httpMethod="GET", tags = {"客户管理"})
    @ApiImplicitParam(name = "client",value = "封装的客户列表实体类",required = true)
    @GetMapping("/findAll")
    public TableDataInfo findAll(Client client) {
        startPage();
        System.out.println(client.toString());
        List<Client> all = clientService.findAll(client);
        return getDataTable(all);
    }


    @ApiOperation(value = "通过客户id查询单条客户信息",response= Integer.class, httpMethod="GET", tags = {"客户管理"})
    @GetMapping("/findOne/{id}")
    public AjaxResult findOne(@PathVariable Integer id) {
        Client one = clientService.findOne(id);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",one);
    }


    @ApiOperation(value = "导出所有客户列表",response= Client.class, httpMethod="GET", tags = {"客户管理"})
    @GetMapping("/exportList")
    public TableDataInfo exportList() {
        startPage();
        List<Client> all = clientService.exportList();
        return getDataTable(all);
    }
}
